package pl.edu.mimuw.irs.core.model.analysis.workflow;

import pl.edu.mimuw.irs.core.global.IDManager;
import pl.edu.mimuw.irs.core.model.design.net.IRSWorkflow;
import pl.edu.mimuw.irs.core.model.petrinet.Place;
import pl.edu.mimuw.irs.core.model.petrinet.Transition;
import pl.edu.mimuw.irs.core.model.petrinet.workflow.Workflow;
import pl.edu.mimuw.irs.core.util.Position;

public class GlobalElementsWorkflow extends Workflow {

	private static final long serialVersionUID = 3544370624850235443L;
	
	// *** Attributes
	
	private IRSWorkflow workflow;
	private Place in;
	private Place out;
	private Transition initialize;
	private Transition finalize;
	private Place mutex;
	private Transition reset;
	// Getters and setters
	public IRSWorkflow getWorkflow() {
		return workflow;
	}
	public void setWorkflow(IRSWorkflow workflow) {
		this.workflow = workflow;
	}
	public Place getIn() {
		return in;
	}
	public void setIn(Place in) {
		if (this.in != null) removeNode(this.in);
		addPlace(in);
		this.in = in;
	}
	public Place getOut() {
		return out;
	}
	public void setOut(Place out) {
		if (this.out != null) removeNode(this.out);
		addPlace(out);
		this.out = out;
	}
	public Transition getInitialize() {
		return initialize;
	}
	public void setInitialize(Transition initialize) {
		if (this.initialize != null) removeNode(this.initialize);
		addTransition(initialize);
		this.initialize = initialize;
	}
	public Transition getFinalize() {
		return finalize;
	}
	public void setFinalize(Transition finalize) {
		if (this.finalize != null) removeNode(this.finalize);
		addTransition(finalize);
		this.finalize = finalize;
	}
	public Place getMutex() {
		return mutex;
	}
	public void setMutex(Place mutex) {
		if (this.mutex != null) removeNode(this.mutex);
		addPlace(mutex);
		this.mutex = mutex;
	}
	public Transition getReset() {
		return reset;
	}
	public void setReset(Transition reset) {
		if (this.reset != null) removeNode(this.reset);
		addTransition(reset);
		this.reset = reset;
	}
	
	// *** Constructors
	
	public GlobalElementsWorkflow(IRSWorkflow workflow) {
		IDManager idManager = getIdManager();
		setWorkflow(workflow);
		setIn(new Place(idManager, idManager.getPrefixedNewId("in")));
		setInitialize(new Transition(idManager, idManager.getPrefixedNewId("initialize")));
		addArc(getIn(), getInitialize());
		setMutex(new Place(idManager, idManager.getPrefixedNewId("mutex")));
		addArc(getInitialize(), getMutex());
		setFinalize(new Transition(idManager, idManager.getPrefixedNewId("finalize")));
		addArc(getMutex(), getFinalize());
		setOut(new Place(idManager, idManager.getPrefixedNewId("out")));
		addArc(getFinalize(), getOut());
		setReset(new Transition(idManager, idManager.getPrefixedNewId("reset")));
		addArc(getOut(), getReset());
		addArc(getReset(), getIn());
		layout();
	}
	
	// *** Methods
	
	protected void layout() {
		Position start = new Position(10, 10);
		Position end = new Position(710, 510);
		getIn().setPosition(start);
		getOut().setPosition(end);
		getInitialize().setPosition(start.right().under());
		getFinalize().setPosition(end.left().over());
		getMutex().setPosition(new Position(end.getX(), start.getY()));
		getReset().setPosition(new Position(start.getX(), end.getY()));
	}
	
}
